package com.max.eshop.UI.userinterface;

import com.max.eshop.UI.executer.Executer;

public class Navigator {
	Painter painter = new Painter();
	Builder build = new Builder();
	Input input = new Input();
	Executer executer = new Executer();
	private final static String nextMenu = "**************************************************";
	private Menu menu;
	private Menu parrentMenu;

	public Menu getParrentMenu() {
		return parrentMenu;
	}

	public void setParrentMenu(Menu parrentMenu) {
		this.parrentMenu = parrentMenu;
	}

	public Navigator() {
		build.buildMenu();
		this.setMenu(build.getFirstMenu());

	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
		painter.outputMenu(menu.getButtons());

	}

	public void nextMenu() {
		Integer next;
		do {
			next = input.inputInformation();
			System.out.println(nextMenu);
		} while (menu.getButtons().size() + 1 <= next);
		if (menu.getButtons().get(next-1).getAction().equals(Action.exit)) {
			System.out.println("Good Bye");
		} else {
			System.out.println(nextMenu);
			if (menu.getButtons().get(next-1).getAction().equals(Action.next)) {
				this.setParrentMenu(getMenu());
				this.setMenu(menu.getButtons().get(next - 1).getNextMenu());
				System.out.println(nextMenu);
				this.nextMenu();
			} else if (menu.getButtons().get(next - 1).getAction()
					.equals(Action.Back)) {
				System.out.println(nextMenu);
				this.setMenu(getParrentMenu());
				System.out.println(nextMenu);
				this.nextMenu();
			} else {
				executer.execute(menu.getButtons().get(next - 1).getAction());
				System.out.println(nextMenu);
				this.setMenu(menu);
				System.out.println(nextMenu);
				this.nextMenu();
			}

		}
	}
}
